package com.staff.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.common.util.AJAXResultMessage;
import com.staff.dao.StaffResumeRepository;
import com.staff.entity.Staff;
import com.staff.entity.StaffResume;
import com.staff.entity.dto.StaffResumeQueryDTO;

@Service
@Transactional
public class StaffResumeService implements IStaffResumeService {
	@Autowired
	private StaffResumeRepository staffResumeRepository;
	@Autowired
	private StaffResumeService staffResumeService;
	@Autowired
	private StaffService staffService;
	
	
	@Override
	public AJAXResultMessage saveOrUpdate(StaffResume staffResume) {
		staffResumeRepository.save(staffResume);
		
		return new AJAXResultMessage(true,"操作成功！");
	}

	@Override
	public void delete(StaffResume staffResume) {
		staffResume=staffResumeRepository.findOne(staffResume.getId());
		if(staffResume!=null) {
			staffResume.setStaff(null);
			staffResumeRepository.save(staffResume);
		}
		staffResumeRepository.delete(staffResume);
	}

	@Override
	public StaffResume findOne(Long id) {
		return staffResumeRepository.findOne(id);
	}

	@Override
	public List<StaffResume> findAll() {
		return (List<StaffResume>) staffResumeRepository.findAll();
	}

	@Override
	public void delete(List<Long> ids) {
		List<StaffResume> staffResumeList = (List<StaffResume>)staffResumeRepository.findAll(ids);
		for(StaffResume staffResume : staffResumeList) {
			staffResume.setStaff(null);
			staffResumeRepository.save(staffResume);
		}
		staffResumeRepository.delete(staffResumeList);
		
	}

	@Override
	public Page<StaffResumeQueryDTO> findAll(Specification<StaffResume> spec, Pageable pageable) {
		//将员工履历page转化为员工履历DTOpage
		List<StaffResumeQueryDTO> results = new ArrayList<StaffResumeQueryDTO>();
		Page<StaffResume> page = staffResumeRepository.findAll(spec, pageable);
		for(StaffResume sc:page.getContent()) {
			StaffResumeQueryDTO scDTO = new StaffResumeQueryDTO(sc, sc.getStaff());
        	results.add(scDTO);
		}
		return new PageImpl<StaffResumeQueryDTO>(results, pageable, results.size());
	}

	@Override
	public AJAXResultMessage saveOrUpdate(StaffResumeQueryDTO staffResumeQueryDTO) {
		StaffResume sr = new StaffResume();
		if(staffService.findOne(staffResumeQueryDTO.getStaffNum())!= null) //判断员工是否存在数据库
		{
			if(staffResumeQueryDTO.getId()== null) 
			{//新建履历
				Staff staff = staffService.findOne(staffResumeQueryDTO.getStaffNum());
				
				sr.setStaffNum(staffResumeQueryDTO.getStaffNum());
				sr.setStaffName(staff.getStaffName());
				sr.setCompanyName(staffResumeQueryDTO.getCompanyName());
				sr.setEntryDate(staffResumeQueryDTO.getEntryDate());
				sr.setPost(staffResumeQueryDTO.getPost());
				sr.setQuitDate(staffResumeQueryDTO.getQuitDate());
				sr.setPosition(staffResumeQueryDTO.getPosition());
				sr.setStaff(staff);
				
				return staffResumeService.saveOrUpdate(sr);
			}
			else
			{//修改履历
				sr = staffResumeService.findOne(staffResumeQueryDTO.getId());
				
				sr.setCompanyName(staffResumeQueryDTO.getCompanyName());
				sr.setEntryDate(staffResumeQueryDTO.getEntryDate());
				sr.setPost(staffResumeQueryDTO.getPost());
				sr.setQuitDate(staffResumeQueryDTO.getQuitDate());
				sr.setPosition(staffResumeQueryDTO.getPosition());
				
				return staffResumeService.saveOrUpdate(sr);
			
				
				
				
				
				
				
				
				
				
				
				
				
			}
		}
		return new AJAXResultMessage(false,"该员工不存在");
	}

}
